﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Wizard.Compiler.Tokenizers
{
    internal class SourcePos
    {
        private readonly INamedTextReader mTextReader;
        private int mColumn;
        private int mLine;
        private Language mLanguage;
        private BaseTokenizer mTokenizer;

        public SourcePos(SourceStack sourceStack, INamedTextReader textReader, Language language)
        {
            mLine = 1;
            mTextReader = textReader;
            mLanguage = language;
            switch (language)
            {
                case Language.Basic:
                    mTokenizer = new Tokenizers.BasicTokenizer(this);
                    break;
                case Language.Java:
                    mTokenizer = new Tokenizers.JavaTokenizer(this);
                    break;
                case Language.Pascal:
                    mTokenizer = new Tokenizers.PascalTokenizer(this);
                    break;
            }
        }

        public string Name
        {
            get { return mTextReader.Name; }
        }

        public int Line
        {
            get { return mLine; }
        }

        public int Column
        {
            get { return mColumn; }
        }

        public int ReadChar()
        {
            while (true)
            {
                var result = mTextReader.ReadChar();

                switch (result)
                {
                    case '\n':
                        mLine++;
                        mColumn = 0;
                        return result;
                    case '\r':
                        // loop
                        break;
                    case -1:
                        return result;

                    default:
                        mColumn++;
                        return result;
                }
            }
        }

        public Language Language
        {
            get { return mLanguage; }
        }

        public BaseTokenizer Tokenizer
        {
            get { return mTokenizer; }
        }

    }
}
